package com.weishuang.monitor.employee.jobhandler.impl;

import com.weishuang.monitor.employee.commons.SendUtils;
import com.weishuang.monitor.employee.entity.ServerCpuRealTime;
import com.weishuang.monitor.employee.jobhandler.ServerHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.util.Util;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class ServerCpuRealHandler implements ServerHandler {

    public final static Logger logger = LoggerFactory.getLogger(ServerDiskInfoHandler.class);

    private static final String upServerCpuRealUrl = "/api/serversBaseInfo/upCpuRealTime";

    /**
     * 服务器信息获取
     */
    @Override
    public void run() {
        SystemInfo si = new SystemInfo();
        CentralProcessor processor = si.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        Util.sleep(1000);
        double cpuread = processor.getSystemCpuLoadBetweenTicks(prevTicks) * 100;
        ServerCpuRealTime serverCpuRealTime = new ServerCpuRealTime();
        serverCpuRealTime.setUsegeRate(new BigDecimal(String.valueOf(cpuread)).setScale(2,BigDecimal.ROUND_DOWN));
        serverCpuRealTime.setCreateTime(LocalDateTime.now(ZoneOffset.UTC));
        try {
            String s = SendUtils.sendRequest(upServerCpuRealUrl, serverCpuRealTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
